import {Context, Next} from 'koa';
import Result from '../vo/result';
import {ResultCode} from '../enums/resultCode';
import logger from "../Logger/logger";

const commonErrorFilter = async (ctx: Context, next: Next) => {
  try {
    await next();
  } catch (err) {
    const timeStamp = new Date().getTime();
    console.log(err)
    logger.error(`${err}`);
    //console.log([object: {"status":400,"desc":"仅支持上传jpg或png类型的文件"}])
    ctx.status = err.status || 500;
    ctx.body = {...Result.fail(ResultCode.FAIL, err.desc), timeStamp, url: ctx.url};
  }
};

export default commonErrorFilter;